<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Result {
	public $code=-1;
	public $message='';
	public $data;
	
	public function setData($code,$message,&$d){
		$this->code=$code;
		$this->message=urlencode($message);
		if(!empty($d)){
//			if(is_object($d)){
//				foreach($d as $n=>$v){
//					$d->$n=urlencode($v);
//				}
//			}elseif(is_array($d)){
//				$count=sizeof($d);
//				echo "count".$count;
//				for($i=0;$i<$count;$i++){
//					foreach($d[$i] as $n=>$v){
//						$d[$i]->$n=urlencode($v);
//					}
//				}
//			}

			$d=$this->convertData($d);
			$this->data=$d;
		}else{
			$this->data=null;
		}
		
	}

	public function convertData($d){
		if(is_object($d)){
			foreach($d as $n=>$v){
				$d->$n=$this->convertData($v);
			}
		}elseif(is_array($d)){
			foreach($d as $k=>$v){
//				echo "key=>".$k.", value=>".$v." <br>";
				$d[urlencode($k)]=$this->convertData($v);
//				echo "key=>".$k.", value=>".$d[urlencode($k)]." <br>";
//				echo "key=>".$k.", value=>".urldecode(json_encode($d[urlencode($k)]))." <br>";
			}
//			echo "array=>".urldecode(json_encode($d))." <br>";
		}else{
			$d=urlencode($d);
		}
		return $d;
	}
	
	public function output(){
		header("Content-Type:text/html;charset=utf8");
		echo urldecode(json_encode(array('code'=>$this->code,'message'=>$this->message,'data'=>$this->data)));
		//echo json_encode(array('code'=>$this->code,'message'=>$this->message,'data'=>$this->data));
	}
}
?>